<template>
    <div class="app-container">
        <div class="block">
            <el-row :gutter="20">

                <el-col :span="3">
                    <el-select v-model="listQuery.criteria.orderStatus" size="mini" placeholder="订单状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option label="未发货" value="0"></el-option>
                        <el-option label="已发货" value="1"></el-option>
                        <el-option label="已收货" value="2"></el-option>
                        <el-option label="退货中" value="3"></el-option>
                        <el-option label="已退货" value="4"></el-option>
                       

                    </el-select>
                </el-col>

                <el-col :span="3">
                    <el-select v-model="listQuery.criteria.storageStatus" size="mini" placeholder="入库状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option label="未入库" value="0"></el-option>
                        <el-option label="已入库" value="1"></el-option>
                    </el-select>
                </el-col>

                <el-col :span="3">
                    <el-select v-model="listQuery.criteria.supplierId" size="mini" placeholder="供应商">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="item in listSup" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-col>

                <el-col :span="4">
                    <el-input v-model="listQuery.criteria.orderNum" size="mini" placeholder="请输入订单编号"></el-input>
                </el-col>
                <el-col :span="4">
                    <el-input v-model="listQuery.criteria.expressId" size="mini" placeholder="请输入快递单号"></el-input>
                </el-col>


                <el-col :span="4">
                    <el-input v-model="listQuery.criteria.purchaser" size="mini" placeholder="请输入采购人"></el-input>
                </el-col>


                <el-col :span="12" style=" margin-top: 10px;">
                    <el-button type="success" size="mini" icon="el-icon-search" @click.native="search">搜索</el-button>
                    <el-button type="primary" size="mini" icon="el-icon-refresh" @click.native="reset">重置</el-button>
                    <el-button type="warning" size="mini" icon="el-icon-plus"
                        @click.native="addOrderBefore">新增采购单</el-button>
                    <el-button type="primary" size="mini" icon="el-icon-delete" @click.native="flushOrder">刷新订单状态</el-button>
                </el-col>
            </el-row>

        </div>

        <el-row>
            <el-col :span="20">
                <el-table :data="list" v-loading="listLoading" element-loading-text="Loading" border fit
                    highlight-current-row @current-change="handleCurrentChange">

                    <el-table-column label="订单编号">
                        <template slot-scope="scope">
                            {{ scope.row.orderNum }}
                        </template>
                    </el-table-column>
                    <el-table-column label="供应商">
                        <template slot-scope="scope">
                            {{ scope.row.supplierName }}
                        </template>
                    </el-table-column>
                    <el-table-column label="采购人">
                        <template slot-scope="scope">
                            {{ scope.row.purchaser }}
                        </template>
                    </el-table-column>
                    <el-table-column label="采购时间">
                        <template slot-scope="scope">
                            {{ formatDate(scope.row.createTime) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="快递单号">
                        <template slot-scope="scope">
                            {{ scope.row.expressId }}
                        </template>
                    </el-table-column>
                    <el-table-column label="入库时间">
                        <template slot-scope="scope">
                            {{ formatDate(scope.row.storageTime) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="订单总价">
                        <template slot-scope="scope">
                            {{ scope.row.totalPrice / 100 }} 元
                        </template>
                    </el-table-column>


                    <el-table-column label="订单状态">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.orderStatus == 0" type="info">未发货</el-tag>
                            <el-tag v-if="scope.row.orderStatus == 1" >已发货</el-tag>
                            <el-tag v-if="scope.row.orderStatus == 2" type="success">已收货</el-tag>
                            <el-tag v-if="scope.row.orderStatus == 3" type="warning">退货中</el-tag>
                            <el-tag v-if="scope.row.orderStatus == 4" type="danger">已退货</el-tag>
                        </template>

                    </el-table-column>

                    <el-table-column label="入库状态">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.storageStatus == 0" type="danger">未入库</el-tag>
                            <el-tag v-if="scope.row.storageStatus == 1" type="success">已入库</el-tag>
                        </template>

                    </el-table-column>

                    <el-table-column label="操作" min-width="150px">
                        <template slot-scope="scope">
                            <el-button type="primary" size="mini" icon="el-icon-view"
                                @click.native="viewOrder(scope.row)">查看</el-button>
                            <el-button v-if="scope.row.orderStatus == 0 || scope.row.orderStatus == 1" type="primary"
                                size="mini" icon="el-icon-edit" @click.native="backOrder(scope.row.id)">退货</el-button>
                        </template>

                    </el-table-column>




                </el-table>

                <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                    :page-sizes="[10, 20, 50, 100, 500]" :page-size="listQuery.size" :total="total"
                    :current-page.sync="listQuery.current" @size-change="changeSize" @current-change="fetchPage"
                    @prev-click="fetchPrev" @next-click="fetchNext">
                </el-pagination>

            </el-col>
        </el-row>



        <el-dialog :title="formTitle" :visible.sync="formVisible" width="60%">
            <el-form ref="addForm" :model="addForm" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="采购时间" prop="createTime">
                            <el-date-picker v-model="addForm.createTime" align="right" type="datetime"
                                placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">

                        <el-form-item label="供应商" prop="supplierId">
                            <el-select v-model="addForm.supplierId" placeholder="请选择" @change="clearnSelectList">
                                <el-option v-for="item in listSup" :key="item.id" :label="item.name" :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="采购人" prop="purchaser">
                            <el-input v-model="addForm.purchaser" placeholder="采购人"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="addForm.remark" placeholder="备注"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>

                    <el-form-item :class="'form-item-full'">
                        <el-table element-loading-text="Loading" :data="selectList" border fit highlight-current-row
                            @current-change="handleCurrentChange">

                            <el-table-column label="名称">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.name }}</span>
                                </template>
                            </el-table-column>

                            <el-table-column label="商品类型" width="180">
                                <template slot-scope="scope">
                                    <span>{{ getTypeValueDict(scope.row.type) }}</span>
                                </template>
                            </el-table-column>

                            <el-table-column label="商品单价" width="180">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.goodsPrice / 100 }} 元/ {{ scope.row.priceUnit }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="规格">
                                <template slot-scope="scope">
                                    <el-tag :key="tag" v-for="tag in scope.row.specification" size="mini" type="info">
                                        {{ tag }}
                                    </el-tag>
                                </template>
                            </el-table-column>

                            <el-table-column label="数量">
                                <template slot-scope="scope">
                                    <el-input-number v-model="scope.row.goodsNum" @change="handleChangeTotal(scope.row)"
                                        :min="1" :max="999" label="描述文字" size="mini"
                                        style="width: 90%;"></el-input-number>
                                </template>
                            </el-table-column>

                            <el-table-column label="总价：">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.goodsTotalPrice / 100 }} 元</span>
                                </template>
                            </el-table-column>

                            <el-table-column label="操作" width="90">
                                <template slot-scope="scope">
                                    <el-button type="text" icon="el-icon-delete" @click.native="deleteGoods(scope.row)">
                                        移除
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <el-row>
                            <el-col :span="4">
                                <span>总价： {{ totalPrice }} 元</span>
                            </el-col>
                        </el-row>
                        <el-button type="text" icon="el-icon-plus" @click.native="addGoods">添加商品</el-button>

                    </el-form-item>

                </el-row>

                <el-row>
                    <el-form-item>
                        <el-button type="primary" @click.native="saveOrder">提交</el-button>
                        <el-button @click.native="formVisible = false">取消</el-button>
                    </el-form-item>
                </el-row>


            </el-form>
        </el-dialog>

        <el-drawer title="选择商品" :visible.sync="drawer" direction="rtl" size="50%">

            <div class="block" style="margin-left: 16px;">
                <el-row :gutter="16">

                    <el-col :span="8">
                        <el-input v-model="listGoodsQuery.criteria.name" size="mini" placeholder="商品名称"></el-input>
                    </el-col>
                    <el-col :span="8">
                        <el-select v-model="listGoodsQuery.criteria.type" size="mini" placeholder="商品类型">
                            <el-option label="全部" value=""></el-option>
                            <el-option v-for="item in goodsType" :key="item.id" :label="item.name"
                                :value="item.num"></el-option>
                        </el-select>
                    </el-col>
                    <br>
                    <el-col :span="16" style=" margin-top: 10px;">
                        <el-button type="success" size="mini" icon="el-icon-search"
                            @click.native="searchGoods">搜索</el-button>
                        <el-button type="primary" size="mini" icon="el-icon-refresh"
                            @click.native="resetGoods">重置</el-button>
                    </el-col>
                </el-row>

                <el-table ref="multipleSelection" :data="listGoods" row-key="id" tooltip-effect="dark"
                    style="width: 100%" @selection-change="handleSelectionChange">
                    <el-table-column :reserve-selection="true" type="selection" align="center"
                        width="90"></el-table-column>
                    <el-table-column prop="name" label="商品名称" width="180"></el-table-column>
                    <el-table-column label="商品类型" width="180">
                        <template slot-scope="scope">
                            <span>{{ getTypeValueDict(scope.row.type) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="规格">
                        <template slot-scope="scope">
                            <el-tag :key="tag" v-for="tag in scope.row.specification" size="mini" type="info">
                                {{ tag }}
                            </el-tag>
                        </template>
                    </el-table-column>

                    <el-table-column label="价格" width="180">
                        <template slot-scope="scope">
                            {{ scope.row.price / 100 }} 元 / {{ scope.row.priceUnit }}
                        </template>
                    </el-table-column>

                </el-table>

                <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                    :page-sizes="[10, 20, 50, 100, 500]" :page-size="listGoodsQuery.size" :total="totalGoods"
                    :current-page.sync="listGoodsQuery.current" @size-change="changeSizeGoods"
                    @current-change="fetchPageGoods" @prev-click="fetchPrevGoods" @next-click="fetchNextGoods">
                </el-pagination>

                <el-button type="primary" @click="confirmSelect">确认选中</el-button>

            </div>


        </el-drawer>

        <el-dialog title="采购订单详情" :visible.sync="infoVisible">

            <el-descriptions title="采购订单">
                <el-descriptions-item label="订单编号" :span="24">
                    <span>{{infoForm.orderNum}}</span>
                     <span style="color: dodgerblue;" type="text" @click="copyTextToClipboard(infoForm.orderNum)"> （ 复制 ）</span>
                </el-descriptions-item>
                <el-descriptions-item label="供应商">{{ infoForm.supplierName }}</el-descriptions-item>
                <el-descriptions-item label="采购日期">{{formatTime(infoForm.createTime)}}</el-descriptions-item>
                <el-descriptions-item label="快递单号">{{ infoForm.expressId }}</el-descriptions-item>
                <el-descriptions-item label="采购人">
                    {{ infoForm.purchaser }}
                </el-descriptions-item>
                <el-descriptions-item label="订单总价">{{ infoForm.totalPrice/100 }} 元</el-descriptions-item>
                <el-descriptions-item label="备注">{{ infoForm.remark}} </el-descriptions-item>
            </el-descriptions>
            <el-descriptions title="订单状态">
                <el-descriptions-item label="物流状态">
                    <el-tag v-if="infoForm.orderStatus == 0" size="mini">未发货</el-tag>
                    <el-tag v-if="infoForm.orderStatus == 1" size="mini">已发货</el-tag>
                    <el-tag v-if="infoForm.orderStatus == 2" size="mini">已收货</el-tag>
                    <el-tag v-if="infoForm.orderStatus == 3" size="mini">退货中</el-tag>
                    <el-tag v-if="infoForm.orderStatus == 4" size="mini">已退货</el-tag>
                   
                </el-descriptions-item>
                <el-descriptions-item label="入库状态">
                    <el-tag v-if="infoForm.storageStatus == 0" size="mini">未入库</el-tag>
                    <el-tag v-if="infoForm.orderStatus == 1" size="mini">已入库</el-tag>
                </el-descriptions-item>
            </el-descriptions>

            <el-descriptions title="商品明细" width="100%" :span="24">
              
          
            </el-descriptions>
            <el-row class="table-header">
                <el-col :span="8">
                    商品
                </el-col>
                <el-col :span="8">
                    规格
                </el-col>
                <el-col :span="4">
                    单价
                </el-col>
                <el-col :span="2">
                    数量
                </el-col>
                <el-col :span="2">
                    合计
                </el-col>
                
          </el-row>
          <el-row v-for="item in infoGoodsList" :key="item.id" class="data-row">
            <el-col :span="8">
                <span style="text-align: left; margin-left: 1px; width: 100%;">
                <img :src="`${baseUrl}file/getFileByGoodsId/${item.goodsId}?${generNum()}`" style="max-width: 50px; max-height: 50px;" alt="加载失败" >
                <span style="display:inline;">{{item.goodsName}}</span>
            </span>
            </el-col>
            <el-col :span="8"><el-tag :key="tag" v-for="tag in item.specification" size="mini" >{{ tag }}</el-tag> </el-col>
            <el-col :span="4">{{item.goodsPrice/100}} 元/{{ item.priceUnit }}</el-col>
            <el-col :span="2">{{item.goodsNum}}</el-col>
            <el-col :span="2">{{item.goodsTotalPrice/100}} 元</el-col>
          </el-row>

        </el-dialog>


    </div>

</template>

<script src="./order.js"></script>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>
<style scoped>
.form-item-full {
    flex: 100% !important;
}
/* 设置整个表格的边框 */
.table-header,
.data-row {
  border-bottom: 1px solid #ebeef5;
  padding: 10px 0;
}

/* 表头样式 */
.table-header {
  font-weight: bold;
  background-color: #f5f7fa;
}

/* 鼠标悬停时的数据行背景颜色 */
.data-row:hover {
  background-color: #f5f7fa;
}

/* 单元格间距 */
.el-col {
  padding: 5px 10px;
}

/* 规格列中的标签间距 */
.el-tag + .el-tag {
  margin-left: 5px;
}
</style>